﻿using UnityEngine;
using System.Collections;
using TNet;

public class PlayerMove : TNBehaviour {

    public static PlayerMove Instance;

    /// <summary>
    /// 目标位置
    /// </summary>
    private Vector3 _target;
    public Vector3 Target
    {
        set
        {
            tno.Send("OnSetTarget", TNet.Target.AllSaved, value);
        }
    }

    public float Speed = 5f;

    void Awake() {
        if (TNManager.isThisMyObject)
        {
            PlayerMove.Instance = this;
        }
    }

    void Update() {
        this.transform.position = Vector3.Lerp(this.transform.position, this._target, Time.deltaTime * this.Speed);
    }

    [RFC]
    void OnSetTarget(Vector3 v) {
        this._target = v;
    }

    /// <summary>
    /// 有新角色加入时，初始化已有角色的位置
    /// </summary>
    /// <param name="p"></param>
    void OnNetworkPlayerJoin(Player p) {
        tno.Send("OnSetTargetImmediate", TNet.Target.AllSaved, transform.position);
    }

    [RFC]
    void OnSetTargetImmediate(Vector3 v) {
        this.transform.position = v;
    }
}
